/*
 * File:   Menu.cpp
 * Author: agu
 *
 * Created on 26 de mayo de 2014, 01:42
 */

#include "Menu.h"
#include "Directories.h"
#include "Sound.h"
#include "Game.h"
#include <string>
#include <vector>

using std::vector;
using std::string;

Menu::Menu(string name) :
window(name) {
    if (!this -> window.init()) {
        throw InitializationException("Error al iniciar al juego");
    }

    this -> renderer = SDL_CreateRenderer(window.getWindowSDL(), -1, SDL_RENDERER_ACCELERATED);
    this -> runnig = false;
    this -> wall = NULL;

    this -> loadGraphics();
    this -> loadAudio();
}

Menu::~Menu() {
    this -> deleteWall();
    this -> deleteButtons();
    this -> deleteAudio();
    this -> deleteLabels();
    SDL_DestroyRenderer(this -> renderer);
}

void Menu::run() {
    this -> runnig = true;

    //while (this -> runnig) {
    while(this->window.isActive()){
        this -> input();
        this -> update();
        this -> render();
    }
}

void Menu::loadGraphics() {
    this -> loadButtons();
    this -> loadWall();
    this -> loadLabels();
}

void Menu::loadLabels() {
    this->labels.push_back(new Label(DIR_IMG_MENU_LABELS + "label.png", this->renderer, 0, 0));
    // this->labels.push_back(new Label(DIR_IMG_MENU_LABELS + "label.png",
    // this->renderer, this->window.getWidth() / 3 + 10, 0));
    this->labels.at(0)->setText(DIR_FONTS + "hda.ttf", 25, Color::black());
    // this->labels.at(1)->setText(DIR_FONTS + "hda.ttf", 25, Color::red());
    this -> inputs.push_back(new Input(DIR_IMG_MENU_LABELS + "label.png", this -> renderer, 0, 40));
    this -> inputs.at(0) -> setText(DIR_FONTS + "crazy.ttf", 28, Color::black());
}

void Menu::loadWall() {
    this -> wall = new Texture(this -> renderer, DIR_IMG_MENU_BACKGROUND + "wall.png", 0, 0);
}

void Menu::loadButtons() {
    this -> buttons.push_back(new Button(this -> renderer, DIR_IMG_MENU_BUTTONS + "iniciar.png",
            DIR_AUDIO + "medium.wav", window.getWidth() / 3, 100));
    this -> buttons.push_back(new Button(this -> renderer, DIR_IMG_MENU_BUTTONS + "iniciar.png", DIR_AUDIO + "low.wav",
            window.getWidth() / 3, 200));
    this->buttons.at(0)->setRunner(new Game(this->window,this->renderer));
}

void Menu::loadAudio() {
    this -> music.load(DIR_AUDIO + "daft.wav");
}

void Menu::input() {
    while (SDL_PollEvent(&this -> event)) {
        this -> handleEvent();
    }
}

void Menu::handleEvent() {
    switch (this -> event.type) {
        case SDL_QUIT:
            //this -> runnig = false;
            this->window.kill();
            break;

        case SDL_WINDOWEVENT:
            this -> window.handleEvent(this -> event.window);

            break;
    }
}

void Menu::render() {
    // ver si esta linea es necesaria
    SDL_SetRenderDrawColor(this -> renderer, 0xFF, 0xFF, 0xFF, 0);
    SDL_RenderClear(this -> renderer);

    // Renders de las cosas que muestro
    this -> renderWall();
    this -> renderButtons();
    this -> renderLabels();
    SDL_RenderPresent(this -> renderer);
}

void Menu::update() {
    this -> updateWall();
    this -> updateLabels();
    this -> updateButtons();
    this -> updateAudio();
}

void Menu::updateWall() {
}

void Menu::updateButtons() {
    for (int i = 0; i < this -> buttons.size(); i++) {
        instrucciones.push(this->buttons.at(i)->getKeyName());//
        this -> buttons.at(i) -> update(&this -> event);
    }
}

void Menu::updateAudio() {
    this -> music.update();
}

void Menu::updateLabels() {
    this -> inputs.at(0) -> update(&this -> event);

    // int x = 0, y = 0;
    //
    // SDL_GetMouseState(&x, &y);
    // string coordenadas("(" + to_string(x) + "," + to_string(y) + ")");
    this->labels.at(0)->setMessage("Nombre");
    this->labels.at(0);
    // this->labels.at(1)->setMessage("Bomberman vs Plantas vs Zombies");
}

void Menu::deleteWall() {
    if (this -> wall != NULL) {
        delete this -> wall;
    }
}

void Menu::deleteButtons() {
    for (int i = 0; i < this -> buttons.size(); i++) {
        delete this -> buttons.at(i);
    }
}

void Menu::deleteLabels() {
    for (int i = 0; i < this->labels.size(); i++) {
        delete this->labels.at(i);
    }
    for (int i = 0; i < this -> inputs.size(); i++) {
        delete this -> inputs.at(i);
    }
}

void Menu::deleteAudio() {
}

void Menu::renderWall() {
    this -> wall -> resize(window.getWidth(), window.getHeight());
    this -> wall -> render();
}

void Menu::renderLabels() {
    for (int i = 0; i < this->labels.size(); i++) {
        this->labels.at(i)->render();
    }
    for (int i = 0; i < this -> inputs.size(); i++) {
        this -> inputs.at(i) -> render();
    }
}

void Menu::renderButtons() {
    for (int i = 0; i < this -> buttons.size(); i++) {
        this -> buttons.at(i) -> render();
    }
}

queue<string> Menu::getInstrucciones(){
    return this->instrucciones;
}